package com.laijiale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laijiale.Service.EmpService;
import com.laijiale.dto.*;
import com.laijiale.entities.Emp;
import com.laijiale.excel.DeptExcel;
import com.laijiale.excel.EmpExcel;
import com.laijiale.excel.RoomExcel;
import com.laijiale.result.Result;
import com.laijiale.util.EasyExcelUtil;
import com.laijiale.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:59
 */
@Tag(name = "员工控制器")
@RestController
@RequestMapping("/api/v1/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Operation(summary = "添加一条记录")
    @PostMapping("save")
    public Object save(@RequestBody  @Validated EmpInsertDTO empInsertDTO){

        return empService.insert(empInsertDTO);
    }
    @Operation(summary = "查询一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return empService.selectById(id);
    }
    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated EmpUpdateDTO empUpdateDTO){

        return empService.updateById(empUpdateDTO);

    }
    @Operation(summary = "删除一条记录")
    @PostMapping("remove/{id}")
    public Object remove(@PathVariable Long id){

        return empService.logicDeleteById(id);

    }
    @Operation(summary = "批量删除记录")
    @PostMapping("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){

        return empService.logicDeleteByIds(ids);

    }
    @Operation(summary = "全查询记录")
    @GetMapping("list")
    public Object list(){

        return empService.list();

    }
    @Operation(summary = "分页查询记录")
    @PostMapping("page")
    public Object page(@RequestBody  @Validated PageDTO pageDTO){

        return empService.page(pageDTO);

    }
    @Operation(summary = "账号密码登录")
    @PostMapping("loginByAccount")
    public Object loginByAccount(@RequestBody  @Validated LoginDTO dto){

        return empService.loginByAccount(dto);

    }
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<EmpExcel> empExcels = new ArrayList<>();
        List<Emp> emps = empService.list();
        for (Emp emp: emps) {
            EmpExcel empExcel = BeanUtil.copyProperties(emp, EmpExcel.class);
            empExcel.setGender(emp.getGender()==0?"男":emp.getGender()==1? "女" :"保密");
            empExcels.add(empExcel);
        }
        EasyExcelUtil.download(resp, "员工统计表.xlsx", "sheet01",
                EmpExcel.class, empExcels);
    }
    @Operation(summary = "按员工名搜索")
    @PostMapping("pageLikeRealname")
    public Object pageLikeRealname(@RequestBody @Validated EmpPageDTO dto){
        return empService.pageLikeRealname(dto);
    }

    //上传文件不是JSON参数，而是二进制参数，不能用@RequestBody
    @Operation(summary = "上传员工头像")
    @PostMapping("uploadAvatar")
    public Object uploadAvatar( @Validated EmpUploadDTO dto){
        return new Result<>(empService.uploadAvatar(dto));
    }
    @Operation(summary = "重置员工密码")
    @PostMapping("resetPassword/{id}")
    public Object resetPassword(@PathVariable Long id) {
        return new Result<>(empService.resetPassword(id));
    }
    @Operation(summary = "获取我的角色")
    @PostMapping("getRoles/{id}")
    public Object getRoles(@PathVariable Long id) {
        return new Result<>(empService.getRoles(id));
    }
    @Operation(summary = "修改角色")
    @PostMapping("updateRoles")
    public Object updateRoles(@RequestBody @Validated EmpUpdateRolesDTO dto) {
        return empService.updateRoles(dto);
    }
    @Operation(summary = "修改我的密码")
    @PostMapping("updatePassword")
    public Object updatePassword(@RequestBody EmpUpdatePasswordDTO dto,
                                 @RequestHeader("token") String token) {
        dto.setId(JwtUtil.getId(token));
        return empService.updatePassword(dto);
    }






}
